var base = '/www'
var fs = require('fs')
var path = require('path')
var tmpl = require('art-template')
var mimes = {
    '.html': 'text/html;charset=UTF-8',
    '.txt': 'text/plain;charset=UTF-8',
    '.css': 'text/css;charset=UTF-8',
    '.js': 'text/javascript;charset=UTF-8',
    '.jpg': 'image/jpeg',
    '.png': 'image/png',
    '.gif': 'image/gif'
}
module.exports.render = function (pathname, data, res) {
    var abspath = path.resolve(process.cwd() + base + pathname)
    fs.readFile(abspath, function (err, htmlBin) {
        res.writeHead(200, {
            'Content-Type': 'text/html;charset=UTF-8'
        })
        if (err) {
            res.end('服务器错误！')
            return false;
        }
        var newhtml = tmpl.render(htmlBin.toString(), data);
        res.end(newhtml);
    });
}


module.exports.static = function (pathname, res) {
    var abspath = path.resolve(process.cwd() + base + pathname);
    var extname = path.extname(abspath);
    fs.readFile(abspath, function (err, bin) {
        if (err) {
            res.end('服务器错误！')
            return false;
        }
        res.writeHead(200, {
            'Content-Type': mimes[extname]
        })
        res.end(bin);
    });
} 